package cn.tedu.tmall.admin.mall.dao.persist.repository.impl;

import cn.tedu.tmall.admin.mall.dao.persist.mapper.GoodsMapper;
import cn.tedu.tmall.admin.mall.dao.persist.repository.IGoodsReposityory;
import cn.tedu.tmall.admin.mall.pojo.entity.Goods;
import cn.tedu.tmall.admin.mall.pojo.vo.CategoryListLtemVO;
import cn.tedu.tmall.admin.mall.pojo.vo.GoodsListItemVO;
import cn.tedu.tmall.admin.mall.pojo.vo.GoodsStandarVO;
import cn.tedu.tmall.common.pojo.po.GoodsSearchPO;
import cn.tedu.tmall.common.pojo.vo.PageData;
import cn.tedu.tmall.common.util.PageInfoToPageDataConverter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.yaml.snakeyaml.events.Event;

import java.util.List;
@Repository
@Slf4j
public class GoodsReposityory implements IGoodsReposityory {
    @Autowired
    GoodsMapper mapper;
    @Override
    public PageData<GoodsListItemVO> list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<GoodsListItemVO> list = mapper.list();
        PageInfo<GoodsListItemVO> pageInfo=new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public PageData<GoodsListItemVO> listByCategory(Long categoryId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<GoodsListItemVO> list=mapper.listByCategory(categoryId); // list = Page extends ArrayList
        //PageInfo<GoodsListItemVO> pageInfo = new PageInfo<>(list); // 92, 3, 4, 5, 6, 7, 8,
        //PageInfo<GoodsListItemVO> pageInfo = PageInfo.of(list);
        return PageInfoToPageDataConverter.convert(PageInfo.of(list));
    }

    @Override
    public int insert(Goods goods) {
        return mapper.insert(goods);
    }


    @Override
    public PageData<GoodsSearchPO> listSearch(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<GoodsSearchPO> list = mapper.listSearch();
        PageInfo<GoodsSearchPO> pageInfo=new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public int updateById(Goods goods) {
        log.debug("开始执行更新商品的数据访问");
        return mapper.updateById(goods);
    }

    @Override
    public GoodsStandarVO getStandardById(Long id) {
        return mapper.getStandarById(id);
    }
}
